package com.code.splider.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.code.splider.dao.ScorePanluMapper;
import com.code.splider.model.ScorePanlu;
import com.code.splider.service.ScorePanluService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class ScorePanluServiceImpl implements ScorePanluService {
	@Resource
	private ScorePanluMapper scorePanluMapper;

	@Override
	public void saveScorePanlu(String jsonText) {
		List<String> list = null;
		try {
			if (null != jsonText) {
				list = new ArrayList<>();
				// 删除全部
				// scorePanluMapper.deleteAll();
				JSONObject jsonObject = JSONObject.fromObject(jsonText);
				JSONArray jsonArray = jsonObject.getJSONArray("rs");
				int l = jsonArray.size();
				for (int i = 0; i < l; i++) {
					String teams = jsonArray.getString(i);
					JSONObject jsonObject2 = JSONObject.fromObject(teams);
					String id = jsonObject2.getString("id");
					System.out.println("..........id:" + id);
					list.add(id);
					// scorePanluMapper.insert(scorePanlu);
				}
				updateScorePanlu(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void updateScorePanlu(List<String> list) {
		if (list != null && !list.isEmpty()) {
			List<ScorePanlu> lus = this.scorePanluMapper.selectAll();
			if (lus != null && !lus.isEmpty()) {
				List<String> zhibos = new ArrayList<>();
				for (ScorePanlu scorePanlu : lus) {
					zhibos.add(scorePanlu.getZhiboId());
				}
				list.removeAll(zhibos);
				insert(list);
			} else {
				insert(list);
			}
		}

	}

	private void insert(List<String> list) {
		if (list != null && !list.isEmpty()) {
			List<ScorePanlu> results = new ArrayList<>();
			for (String string : list) {
				ScorePanlu lu = new ScorePanlu();
				lu.setZhiboId(string);
				results.add(lu);
			}
			this.scorePanluMapper.insertList(results);
		}
	}
}
